import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';
import '../widgets/docx_viewer_widget.dart';

/// DOCX查看器测试页面
class DocxTestPage extends StatefulWidget {
  @override
  _DocxTestPageState createState() => _DocxTestPageState();
}

class _DocxTestPageState extends State<DocxTestPage> {
  String? _selectedFilePath;
  String? _fileName;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('DOCX查看器测试'),
        actions: [
          IconButton(
            icon: Icon(Icons.folder_open),
            onPressed: _pickDocxFile,
            tooltip: '选择DOCX文件',
          ),
        ],
      ),
      body: _selectedFilePath == null
          ? _buildFileSelector()
          : DocxViewerPage(
              filePath: _selectedFilePath!,
              title: _fileName,
            ),
    );
  }

  Widget _buildFileSelector() {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(
            Icons.description,
            size: 80,
            color: Colors.grey[400],
          ),
          SizedBox(height: 24),
          Text(
            '选择DOCX文件进行查看',
            style: TextStyle(
              fontSize: 18,
              color: Colors.grey[600],
            ),
          ),
          SizedBox(height: 32),
          ElevatedButton.icon(
            onPressed: _pickDocxFile,
            icon: Icon(Icons.folder_open),
            label: Text('选择文件'),
            style: ElevatedButton.styleFrom(
              padding: EdgeInsets.symmetric(horizontal: 32, vertical: 16),
              textStyle: TextStyle(fontSize: 16),
            ),
          ),
          SizedBox(height: 16),
          Text(
            '支持的格式: .docx',
            style: TextStyle(
              fontSize: 14,
              color: Colors.grey[500],
            ),
          ),
        ],
      ),
    );
  }

  Future<void> _pickDocxFile() async {
    try {
      FilePickerResult? result = await FilePicker.platform.pickFiles(
        type: FileType.custom,
        allowedExtensions: ['docx'],
        allowMultiple: false,
      );

      if (result != null && result.files.single.path != null) {
        setState(() {
          _selectedFilePath = result.files.single.path!;
          _fileName = result.files.single.name;
        });
      }
    } catch (e) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text('选择文件失败: $e'),
          backgroundColor: Colors.red,
        ),
      );
    }
  }
} 